package com.company.dao;

import com.company.domain.Student;
import com.company.utils.JDBCUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.PropertyPermission;

public class StudentDaoImpl implements StudentDao{
    @Override
    public ArrayList<Student> findAll() {
        ArrayList<Student> stuList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            pst = con.prepareStatement("SELECT * FROM student");
            rs = pst.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("sid");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                Student s = new Student(id, name, age, birthday);
                stuList.add(s);
//                PropertyDescriptor id = new PropertyDescriptor("id", s.getClass());
//                Method idWriteMethod = id.getWriteMethod();
//                idWriteMethod.invoke(s, rs.getInt("id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con, pst, rs);
        }
        return stuList;
    }

    @Override
    public Student findById(Integer id) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        Student s = null;
        try {
            con = JDBCUtils.getConnection();
            pst = con.prepareStatement("SELECT * FROM student WHERE sid = ?" );
            pst.setInt(1, id);
            rs = pst.executeQuery();
            while (rs.next()) {
                int sid = rs.getInt("sid");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                s = new Student(sid, name, age, birthday);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,pst);
        }
        return s;
    }

    @Override
    public int insert(Student stu, Connection con) {
        PreparedStatement pst = null;
        int rs = 0;
        try {
            pst = con.prepareStatement("INSERT INTO student VALUES (?,?,?,?)");
            int sid = stu.getId();
            String name = stu.getName();
            int age = stu.getAge();
            Date birthday = stu.getBirthday();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(birthday);
            pst.setInt(1, sid);
            pst.setString(2, name);
            pst.setInt(3, age);
            pst.setString(4, format);
            rs = pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, null);
        }
        return rs;
    }

    @Override
    public int update(Student stu) {
        Connection con = null;
        PreparedStatement pst = null;
        int rs = 0;
        try {
            con = JDBCUtils.getConnection();
            pst = con.prepareStatement("UPDATE student SET sid=?,name=?,age=?,birthday=? WHERE sid = ?");
            int sid = stu.getId();
            String name = stu.getName();
            int age = stu.getAge();
            Date birthday = stu.getBirthday();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(birthday);
            pst.setInt(1, sid);
            pst.setString(2, name);
            pst.setInt(3, age);
            pst.setString(4, format);
            pst.setInt(5, sid);
            rs = pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con, pst);
        }
        return rs;
    }

    @Override
    public int delete(Integer id) {
        Connection con = null;
        PreparedStatement pst = null;
        int rs = 0;
        try {
            con = JDBCUtils.getConnection();
            pst = con.prepareStatement("DELETE FROM student WHERE sid = ?");
            pst.setInt(1, id);
            rs = pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con, pst);
        }
        return rs;
    }
}
